<template>
  <div class="container">
    <div class="row">
      <div class="col">
        <h1 class="text-center m-4">{{ msg }}</h1>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { reactive, toRefs } from "@vue/reactivity";
import { onMounted } from "@vue/runtime-core";
import { postRequest, apis } from "@/common/ts/request";
import {getCurrentAddress } from "@/service/tw";
import tp from "tp-js-sdk"
import { Toast } from "vant";
export default {
  setup() {
    onMounted(async () => {
      dlog("onMounted");
      dlog("address:", await getAddress());
    });
    //获取地址
    const getAddress = async () =>{
      let address = ""
      if(tp.isConnected()){
        const {data} = await tp.getCurrentWallet();
        address =  data.address;
        console.log("tp address",address)
      }else{
        address = getCurrentAddress()
        console.log("tronWeb address",address)
      }

      if(!address){
        Toast.fail('获取地址失败，请重试')
      }
      return address
    }

    const state = reactive({
      msg: "loading",
      show: false,
    });

    const testApi = async () => {
      for (const key in apis) {
        try {
          const res = await postRequest(apis[key]);
          console.log(key, res);
        } catch (e) {
          console.log(e);
        }
      }
    };

    const dlog = (...data) => {
      console.log(...data);
      
      // alert(JSON.stringify(data));
    };

    return {
      ...toRefs(state),
    };
  },
};
</script>

<style lang="scss" scoped></style>
